using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;

public class ProjectileEventSubscriber:EventSubscriber<ProjectileEventArgs> {

	protected Dictionary<Projectile,System.EventHandler<ProjectileEventArgs>> singleSubscribtionsShooterProjectile = new Dictionary<Projectile,System.EventHandler<ProjectileEventArgs>>();
	protected Dictionary<Weapon,System.EventHandler<ProjectileEventArgs>> singleSubscribtionsShooterWeapon = new Dictionary<Weapon,System.EventHandler<ProjectileEventArgs>>();
	protected Dictionary<Mob,System.EventHandler<ProjectileEventArgs>> singleSubscribtionsShooterMob = new Dictionary<Mob,System.EventHandler<ProjectileEventArgs>>();

	#region projectile
	public void SubscribeSingleShooterProjectile(Projectile shooter,System.EventHandler<ProjectileEventArgs> handler) {
		if(singleSubscribtionsShooterProjectile.ContainsKey(shooter)) {
			singleSubscribtionsShooterProjectile[shooter]+=handler;
		} else {
			singleSubscribtionsShooterProjectile.Add(shooter,handler);
		}
	}
	public void UnsubscribeSingleShooterProjectile(Projectile shooter,System.EventHandler<ProjectileEventArgs> handler) {
		if(!singleSubscribtionsShooterProjectile.ContainsKey(shooter)) return;

		singleSubscribtionsShooterProjectile[shooter]-=handler;
		if(singleSubscribtionsShooterProjectile[shooter]==null)
			singleSubscribtionsShooterProjectile.Remove(shooter);

	}
	#endregion
	#region mob
	public void SubscribeSingleShooterMob(Mob shooter,System.EventHandler<ProjectileEventArgs> handler) {
		if(singleSubscribtionsShooterMob.ContainsKey(shooter)) {
			singleSubscribtionsShooterMob[shooter]+=handler;
		} else {
			singleSubscribtionsShooterMob.Add(shooter,handler);
		}
	}
	public void UnsubscribeSingleShooterMob(Mob shooter,System.EventHandler<ProjectileEventArgs> handler) {
		if(!singleSubscribtionsShooterMob.ContainsKey(shooter)) return;

		singleSubscribtionsShooterMob[shooter]-=handler;
		if(singleSubscribtionsShooterMob[shooter]==null)
			singleSubscribtionsShooterMob.Remove(shooter);

	}
	#endregion
	#region weapon
	public void SubscribeSingleShooterWeapon(Weapon shooter,System.EventHandler<ProjectileEventArgs> handler) {
		if(singleSubscribtionsShooterWeapon.ContainsKey(shooter)) {
			singleSubscribtionsShooterWeapon[shooter]+=handler;
		} else {
			singleSubscribtionsShooterWeapon.Add(shooter,handler);
		}
	}
	public void UnsubscribeSingleShooterWeapon(Weapon shooter,System.EventHandler<ProjectileEventArgs> handler) {
		if(!singleSubscribtionsShooterWeapon.ContainsKey(shooter)) return;

		singleSubscribtionsShooterWeapon[shooter]-=handler;
		if(singleSubscribtionsShooterWeapon[shooter]==null)
			singleSubscribtionsShooterWeapon.Remove(shooter);

	}
	#endregion

	public override void TargetEventHandler(object sender,ProjectileEventArgs e) {
		base.TargetEventHandler(sender,e);
		if(e.shooterWeapon!=null&&singleSubscribtionsShooterWeapon.ContainsKey(e.shooterWeapon))
			singleSubscribtionsShooterWeapon[e.shooterWeapon].Invoke(sender,e);
		if(e.shooterProjectile&&singleSubscribtionsShooterProjectile.ContainsKey(e.shooterProjectile))
			singleSubscribtionsShooterProjectile[e.shooterProjectile].Invoke(sender,e);
		if(e.shooterMob&&singleSubscribtionsShooterMob.ContainsKey(e.shooterMob))
			singleSubscribtionsShooterMob[e.shooterMob].Invoke(sender,e);
	}

}

public static class ProjectileEventHelper {

	public static ProjectileEventSubscriber firing = new ProjectileEventSubscriber();
	public static ProjectileEventSubscriber fired = new ProjectileEventSubscriber();
	public static ProjectileEventSubscriber hitmob = new ProjectileEventSubscriber();
	public static ProjectileEventSubscriber hitwall = new ProjectileEventSubscriber();
	public static ProjectileEventSubscriber destroy = new ProjectileEventSubscriber();


	static ProjectileEventHelper() {
		Projectile.ProjectileFiring+=firing.TargetEventHandler;
		Projectile.ProjectileFired+=fired.TargetEventHandler;
		Projectile.ProjectileHitMob+=hitmob.TargetEventHandler;
		Projectile.ProjectileHitWall+=hitwall.TargetEventHandler;
		Projectile.ProjectileDestroy+=destroy.TargetEventHandler;
	}
}
